
package mock.ab_sd.web.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import mock.ab_sd.common.valueobject.GovernmentVO;
import mock.ab_sd.daointerface.GovernmentDAOImpl;

/**
 * Store, get info from Database and response Struts 2 Action for
 * ViewGovernmentOfficeRegion.jsp * 
 * @author HAI
 */

@SuppressWarnings("serial")
public class ViewGORAction extends BaseAction {
	/*
	 * This class has 4 attributes with corresponding get/set and execute()
	 * method.
	 */

	/** GORinfo is used to contain data got from DataBase */
	private GovernmentVO GORinfo;

	/** govOfficeRegionName is used to contain name of Government Office Region */
	private String govOfficeRegionName = "";

	/** countyName is used to contain County name */
	private String countyName = "";

	/** description is used to contain description of Government Office Region */
	private String description = "";

	/**
	 * execute() method get ID from ListGovernmentOfficeRegion.jsp and get
	 * corresponding data from Database then return SUCCESS to redirect to
	 * ViewGovernmentOfficeRegion.jsp and display data
	 */
	public String execute() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		/* Declare instance of GovernmentDAO class to call getGovernmentInfo() */
		GovernmentDAOImpl getData = new GovernmentDAOImpl();
		try {
			/* Call getGovernmentInfo() to get Information of Government Office */
			/* Region and assign data to GORinfo */
			this.setGovernmentInfo(getData.getGovernmentInfo(Integer
					.parseInt(request.getParameter("govOfficeRegionID"))));
			/* Set Info to corresponding variables to display */
			setGovOfficeRegionName(GORinfo.getGovOfficeRegionName());
			setCountyName(GORinfo.getCountyName());
			setDescription(GORinfo.getDescription());
			/*
			 * Check returned data if null return ERROR to redirect to
			 * ErrorViewGovernmentOfficeRegion.jsp
			 */
			if ((getCountyName().isEmpty()) || (getDescription().isEmpty())
					|| (getGovOfficeRegionName().isEmpty())) {
				return ERROR;
			}
		} catch (Exception e) {
			/* return ERROR to redirect to ErrorViewGovernmentOfficeRegion.jsp */
			return ERROR;
		}
		/* return SUCCESS to display ViewGovernmentOfficeRegion.jsp */
		return SUCCESS;
	}

	public GovernmentVO getGovernmentInfo() {
		return this.GORinfo;
	}

	public void setGovernmentInfo(GovernmentVO government) {
		this.GORinfo = government;
	}

	public String getGovOfficeRegionName() {
		return govOfficeRegionName;
	}

	public void setGovOfficeRegionName(String govOfficeRegionName) {
		this.govOfficeRegionName = govOfficeRegionName;
	}

	public String getCountyName() {
		return countyName;
	}

	public void setCountyName(String countyName) {
		this.countyName = countyName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
